#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <conio.h>
#include <stdlib.h>
#include <stdbool.h>

#include "Setting.h"
#include "public.h"
#include "RandomMaze.h"

int map[21][42] =
{
        {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
        {-1, 1, 0, 0, 0,-1, 0, 0, 0, 0,-1, 0, 0, 0, 0,-1, 0, 0, 0, 0,-1},
        {-1, 0,-1,-1, 0,-1, 0,-1,-1, 0,-1,-1, 0,-1, 0,-1, 0, 0, 0, 0,-1},
        {-1, 0,-1, 0, 0,-1, 0,-1,-1, 0, 0, 0, 0,-1, 0,-1, 0, 0, 0, 0,-1},
        {-1, 0,-1,-1, 0,-1, 0,-1,-1,-1, 0,-1,-1,-1, 0,-1, 0, 0, 0, 0,-1},
        {-1, 0, 0,-1, 0, 0, 0, 0, 0,-1, 0,-1, 0, 0, 0,-1, 0, 0, 0, 0,-1},
        {-1,-1, 0,-1,-1,-1,-1,-1, 0,-1, 0,-1, 0,-1, 0,-1,-1,-1,-1,-1,-1},
        {-1,-1, 0, 0, 0,-1, 0,-1, 0,-1,-1,-1, 0, 0, 0, 0, 0, 0, 0, 0,-1},
        {-1,-1,-1,-1, 0,-1, 0,-1, 0, 0,-1,-1,-1,-1,-1,-1, 0,-1,-1, 0,-1},
        {-1, 0, 0, 0, 0,-1, 0,-1, 0,-1,-1, 0, 0, 0, 0, 0, 0,-1,-1, 2,-1},
        {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
};

void RandomMazePrint(int x, int y, int num)
{
    //4显示红色
    gotoxy(y * 3 + 1, x + 1);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), num);
    printf("%3d", map[x][y]);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);    //字体显示黑色
}

void RandomMaze()
{
    int x = 1, y = 1;
    int i = 0, j = 0;
    int random = 0, count = 1;
    int start = 1;

    system("cls");
    for (i = 0; i <= LINE; ++i)
    {
        for (j = 0; j <= COLUMN; ++j)
        {
            if (map[i][j] == -1)
            {
                printf("|||");
            }
            else if (i == 0 && j == 0)
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
                printf("%3d", map[i][j]);
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
            }
            else if (map[i][j] != 0)
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);
                printf("%3d", map[i][j]);
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
            }
            else
            {
                printf("%3d", map[i][j]);
            }
        }
        printf("\n");
    }

    srand((unsigned)time(NULL));

    while (start == 1)
    {
        if (map[x][y] == 2)
        {
            //离开循环标志
            start = 0;
            //把最后一个数字变红
            RandomMazePrint(x, y, 4);
            //打印结束语句
            gotoxy(1, LINE + 5);
            printf("Game over!\n");
        }
        else
        {
            random = rand() % 4;
            if (random == 0)x -= 1;
            else if (random == 1)x += 1;
            else if (random == 2)y -= 1;
            else if (random == 3)y += 1;
            if (x > LINE || y > COLUMN || x < 1 || y < 1 || map[x][y] == -1)
            {
                switch (random)
                {
                case 0:x += 1; break;
                case 1:x -= 1; break;
                case 2:y += 1; break;
                case 3:y -= 1; break;
                }
            }
            else
            {
                if (map[x][y] == 2)
                {
                    //离开循环标志
                    start = 0;
                    //把最后一个数字变红
                    RandomMazePrint(x, y, 4);
                    //打印结束语句
                    gotoxy(1, LINE + 5);
                    printf("Game over!\n");
                    break;
                }
                count++;
                map[x][y] = 1;

                RandomMazePrint(x, y, 4);

                gotoxy(1, LINE + 2);
                switch (random)
                {
                case 0:printf("Up\t\n");    break;
                case 1:printf("Down\t\n");  break;
                case 2:printf("Left\t\n");  break;
                case 3:printf("Right\t\n"); break;
                }
                printf("Location:%2dand%2d\nCount:%2d\n", x, y, count);

                //按键检测
                key();

                RandomMazePrint(x, y, 6);
            }
        }
    }
    system("pause");
}
